﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QLTuyenXe.BLL;
using QLTuyenXe.DAO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Data.SqlClient;
using QLTuyenXe.library;


namespace QLTuyenXe.GUI
{
    public partial class frmDoiMatKhau : DevComponents.DotNetBar.Office2007Form
    {
        int i = 0;
        public static string username1 ="";
        BUser user = new BUser();
        XuLyDL xl = new XuLyDL();
        ThongBao thongBao = new ThongBao();
        public frmDoiMatKhau()
        {
            InitializeComponent();
        }
        
        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (MessageBoxEx.Show("Bạn thật sự muốn thoát không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void btnDoiMatKhau_Click(object sender, EventArgs e)
        {
            if (txtMatKhauCu.Text != "" && txtMatKhauMoi.Text != "" && txtNhapLaiMKMoi.Text != "")
            {
                if (CheckPass(txtMatKhauMoi.Text) == false)
                {
                    if (Kiemtra(txtMatKhauMoi.Text, txtNhapLaiMKMoi.Text) == 0)
                    {
                        DataTable tb = user.Select_DN("name='" + username1 + "'", "password='" + xl.GetMD5(txtMatKhauCu.Text) + "'");
                        if (tb.Rows.Count > 0)
                        {
                            int kq = 0;
                            USER us = new USER();
                            us.Password = xl.GetMD5(txtMatKhauMoi.Text);
                            kq = user.Update_MK(us, "name='" + username1 + "'");
                            if (kq > 0)
                            {
                                thongBao.Sua();
                            }
                            else
                            {
                                thongBao.LoiSua();
                            }
                        }
                        else
                        {
                            if (MessageBoxEx.Show("Bạn nhập mật khẩu cũ sai", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
                            {
                                txtMatKhauCu.Clear();
                                txtMatKhauMoi.Clear();
                                txtNhapLaiMKMoi.Clear();
                                txtMatKhauCu.Focus();
                            }
                            else
                            {
                                this.Close();
                            }
                        }
                    }
                }
            }
        }
        
        private void frmDoiMatKhau_Load(object sender, EventArgs e)
        {
    
        }
        public int Kiemtra(string a, string b)
        {
            //int i = 0;
            if (String.Compare(a, b) != 0)
            {
                thongBao.KTNhapLaiMK();
                return i = 1;
            }
            else
            {
                return i = 0;
            }
        }

        private void txtNhapLaiMKMoi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnDoiMatKhau_Click(sender, e);
            }
        }

        public bool CheckPass(string pass)
        {
            if (pass.Length <= 3)
            {
                thongBao.KTMatKhau();
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
